package com.zyy.smartcity1129.movie;

import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.gson.JsonObject;
import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.data.MovieCommentsInfo;
import com.zyy.smartcity1129.data.MovieNewsInfo;
import com.zyy.smartcity1129.data.ResultInfo;
import com.zyy.smartcity1129.util.Http;
import com.zyy.smartcity1129.util.RvAd;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

import okhttp3.RequestBody;

/**
 * @ClassName: MovieNewsFragment$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 18/11/2021$
 * @Version: 1.0
 */
public class MovieNewsFragment extends BaseFragment {
    private String newname = MovieMainFragment.names;
    private int newId = MovieMainFragment.newsId;
    private TextView textView2, textView3, textView4, textView5, textView6, textView7, textView8;
    private ImageView imageView;
    private WebView webView;
    private RecyclerView rv;
    private Button btn;
    private EditText edit;
    ExecutorService executor = Executors.newFixedThreadPool(2);
    MutableLiveData<AtomicInteger> mData = new MutableLiveData<>();
    AtomicInteger mInteger = new AtomicInteger(0);
    MovieNewsInfo.DataBean dataBean;
    List<MovieCommentsInfo.RowsBean> commentsList = new ArrayList<>();
    RvAd<MovieCommentsInfo.RowsBean> rvAd;
    @Override
    protected int layId() {
        return R.layout.activity_movie_news;
    }

    @Override
    protected String title() {
        return newname;
    }

    @Override
    protected boolean back() {
        return true;
    }

    @Override
    protected Boolean bottom() {
        return false;
    }

    @Override
    protected void init(View v) {
        initView(v);
        executor.execute(new Runnable() {
            @Override
            public void run() {
                dataBean = Http.getInstance().get("/prod-api/api/movie/press/press/" + newId, null, MovieNewsInfo.class, 1).getData();
                mInteger.getAndIncrement();
                mData.postValue(mInteger);
            }
        });
        executor.execute(new Runnable() {
            @Override
            public void run() {
                commentsList = Http.getInstance().get("/prod-api/api/movie/press/comments/list", null, MovieCommentsInfo.class, 1).getRows();
                mInteger.getAndIncrement();
                mData.postValue(mInteger);
            }
        });
        mData.observe(this, atomicInteger -> {
            if (atomicInteger.get() == 2) {
                List<MovieCommentsInfo.RowsBean> comList = new ArrayList<>();
                comList.addAll(commentsList.stream().filter(a -> a.getNewsTitle().equals(newname)).collect(Collectors.toList()));
                initAdapter(comList);
                Glide.with(imageView.getContext()).load(Http.base + dataBean.getCover()).into(imageView);
                textView2.setText(dataBean.getTitle());
                textView3.setText("上映时间：" + dataBean.getPublishDate());
                textView4.setText(String.valueOf(dataBean.getReadNum()));
                textView5.setText(String.valueOf(dataBean.getLikeNum()));
                textView6.setText(String.valueOf(dataBean.getCommentNum()));
                textView7.setText("创建时间：" + dataBean.getCreateTime());
                textView8.setText("更新时间：" + dataBean.getUpdateTime());
                rv.setLayoutManager(new LinearLayoutManager(getContext()));
                rv.setAdapter(rvAd);
                webView.loadDataWithBaseURL(Http.base, dataBean.getContent(), "text/html", "utf-8", null);
            }
        });
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String myComment = edit.getText().toString().trim();
                if (!myComment.equals("")){
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            JsonObject jsonObject = new JsonObject();
                            jsonObject.addProperty("newsId",newId);
                            jsonObject.addProperty("content",myComment);
                            String result = Http.getInstance().get("/prod-api/api/movie/press/pressComment", RequestBody.create(Http.m, jsonObject.toString()), ResultInfo.class,2).getMsg();
                            commentsList = Http.getInstance().get("/prod-api/api/movie/press/comments/list", null, MovieCommentsInfo.class, 1).getRows();
                            List<MovieCommentsInfo.RowsBean> comList = new ArrayList<>();
                            comList.addAll(commentsList.stream().filter(a -> a.getNewsTitle().equals(newname)).collect(Collectors.toList()));
                            getActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    initAdapter(comList);
                                    rv.setAdapter(rvAd);
                                }
                            });
                        }
                    }).start();
                    rvAd.notifyDataSetChanged();
                    rv.setAdapter(rvAd);
                }
            }
        });
    }

    private void initAdapter(List<MovieCommentsInfo.RowsBean> comList) {
        rvAd = new RvAd<MovieCommentsInfo.RowsBean>(comList, R.layout.rv_moviecomment) {
            private TextView tv1;
            private WebView web2;
            private TextView tv3;
            private TextView tv4;
            private LinearLayout rv_line;
            private void initView(View v) {
                tv1 = (TextView) v.findViewById(R.id.tv1);
                web2 = (WebView) v.findViewById(R.id.web2);
                tv3 = (TextView) v.findViewById(R.id.tv3);
                tv4 = (TextView) v.findViewById(R.id.tv4);
                rv_line = (LinearLayout) v.findViewById(R.id.rv_linecomment);
            }
            @Override
            protected void initR(View iv, MovieCommentsInfo.RowsBean rowsBean, int p) {
                initView(iv);
                tv1.setText(rowsBean.getUserName());
                web2.loadDataWithBaseURL(Http.base, rowsBean.getContent(), "text/html", "utf-8", null);
                tv3.setText(rowsBean.getCommentDate());
                tv4.setText(String.valueOf(rowsBean.getLikeNum()));
                rv_line.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                                Http.getInstance().get("/prod-api/api/movie/press/pressComment/like/" + rowsBean.getId(), RequestBody.create("",Http.m),null,3);
                    }
                });
            }
        };
    }

    private void initView(View v) {
        textView2 = v.findViewById(R.id.textView2);
        textView3 = v.findViewById(R.id.textView3);
        textView4 = v.findViewById(R.id.textView4);
        textView5 = v.findViewById(R.id.textView5);
        textView6 = v.findViewById(R.id.textView6);
        textView7 = v.findViewById(R.id.textView7);
        textView8 = v.findViewById(R.id.textView8);
        imageView = v.findViewById(R.id.imageView);
        webView = v.findViewById(R.id.webViewHospital);
        rv = v.findViewById(R.id.rv);
        btn = v.findViewById(R.id.btn);
        edit = v.findViewById(R.id.edit);
    }
}
